package com.ea.report.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ea.common.SA.FaceRegService;
import com.ea.common.SA.pojo.param.PersonFaceIn;
import com.ea.common.result.Result;
import com.ea.report.mapper.UserFaceMapper;
import com.ea.report.pojo.po.UserFace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/report/super/test")
public class SuperAdminTestController {
    private static final Logger log = LoggerFactory.getLogger(SuperAdminTestController.class);
    @Autowired
    UserFaceMapper userFaceMapper;
    @Autowired
    FaceRegService faceRegService;

    // 删除 user face 脏数据
    @GetMapping("/deleteUserFaceDirtyData/{count}")
    public Result<String> deleteUserFaceDirtyData(@PathVariable("count") int count) {
        // 获得当前系统所有的人脸信息
        StringBuilder sb = new StringBuilder();
        int deleteUserFaceCount = 0;
        List<UserFace> userFaceList = userFaceMapper.selectList(new LambdaQueryWrapper<>());
        for (UserFace userFace : userFaceList) {
            if (count <= 0) {
                break;
            }
            // 如果人脸信息为空（没有保存到人脸信息库）则删除
            if (faceRegService.getFace(userFace.getUserFaceId()) == null) {
                userFaceMapper.deleteById(userFace);
                String info = "删除人脸信息：" + userFace.getUserFaceId();
                sb.append(info).append("\n");
                count--;
                deleteUserFaceCount++;
            }
        }
        log.info("删除UserFace总数：{}", deleteUserFaceCount);
        log.info("删除UserFace信息：{}", sb);
        return Result.success(sb.toString());
    }

    // 删除 face reg 脏数据
    @GetMapping("/deleteFaceRegDirtyData/{count}")
    public Result<String> deleteFaceRegDirtyData(@PathVariable("count") int count) {
        // 获得当前系统所有的人脸信息
        StringBuilder sb = new StringBuilder();
        int deleteFaceRegCount = 0;
        List<PersonFaceIn> personFaceInList = faceRegService.getAllFaces();
        for (PersonFaceIn personFaceIn : personFaceInList) {
            log.info("personFaceIn:{}", personFaceIn);
            if (count <= 0) {
                break;
            }
            // 如果人脸信息为空（没有保存到人脸信息库）则删除
            if (userFaceMapper.selectById(personFaceIn.getPersonFaceId()) == null) {
                faceRegService.removeFace(personFaceIn.getPersonFaceId());
                String info = "删除人脸信息：" + personFaceIn.getPersonFaceId();
                sb.append(info).append("\n");
                count--;
                deleteFaceRegCount++;
            }
        }
        log.info("删除FaceReg总数：{}", deleteFaceRegCount);
        log.info("删除FaceReg信息：{}", sb);
        return Result.success(sb.toString());
    }
}
